﻿using Microsoft.Win32;

namespace TroubleshootingofBeanBagAssistant
{
	/// <summary>
	/// 系统麦克风
	/// </summary>
	public static class SystemMicrophone
	{
		// 检查“允许应用访问你的麦克风”
		public static bool IsAppMicrophoneAccessAllowed()
		{
			try
			{
				using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone", false);
				if (key != null)
				{
					var value = key.GetValue("Value") as string;
					return string.Equals(value, "Allow", StringComparison.OrdinalIgnoreCase);
				}
			}
			catch { }
			return false;
		}

		public static void EnableDeviceMicrophoneAccess()
		{
			try
			{
				using var devicesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture", true);
				if (devicesKey != null)
				{
					foreach (var deviceId in devicesKey.GetSubKeyNames())
					{
						using var deviceKey = devicesKey.OpenSubKey(deviceId, true);
						if (deviceKey != null)
						{
							deviceKey.SetValue("DeviceState", 1, RegistryValueKind.DWord); // 1=启用
						}
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"设置设备麦克风权限失败: {ex.Message}");
			}
		}


		// 设置“允许应用访问你的麦克风”为允许
		public static void EnableAppMicrophoneAccess()
		{
			try
			{
				using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone", true);
				if (key != null)
				{
					key.SetValue("Value", "Allow", RegistryValueKind.String);
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"设置应用麦克风权限失败: {ex.Message}");
			}
		}

		// 检查“允许桌面应用访问麦克风”
		public static bool IsDesktopMicrophoneAccessAllowed()
		{
			try
			{

				using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone\NonPackaged", false);
				if (key != null)
				{
					foreach (var subKeyName in key.GetSubKeyNames())
					{
						using var subKey = key.OpenSubKey(subKeyName, false);
						var value = subKey?.GetValue("Value") as string;
						if (string.Equals(value, "Deny", StringComparison.OrdinalIgnoreCase))
							return false; // 只要有一个被拒绝就算未开启
					}
				}
			}
			catch { }
			return true;
		}


		// 设置“允许桌面应用访问麦克风”为允许
		public static void EnableDesktopMicrophoneAccess()
		{
			try
			{
				using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone\NonPackaged", true);
				if (key != null)
				{
					foreach (var subKeyName in key.GetSubKeyNames())
					{
						using var subKey = key.OpenSubKey(subKeyName, true);
						if (subKey != null)
							subKey.SetValue("Value", "Allow", RegistryValueKind.String);
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"设置桌面应用麦克风权限失败: {ex.Message}");
			}
		}

		public static bool IsDeviceMicrophoneAccessAllowed()
		{
			try
			{
				using var devicesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture", false);
				if (devicesKey != null)
				{
					foreach (var deviceId in devicesKey.GetSubKeyNames())
					{
						using var deviceKey = devicesKey.OpenSubKey(deviceId, false);
						if (deviceKey != null)
						{
							object stateObj = deviceKey.GetValue("DeviceState");
							if (stateObj is int state)
							{
								if (state != 1) // 1=启用，4=禁用
									return false;
							}
							else if (stateObj is null)
							{
								// 没有 DeviceState 视为未开启
								return false;
							}
						}
					}
				}
			}
			catch
			{
				// 读取失败视为未开启
				return false;
			}
			// 所有设备都为启用
			return true;
		}

	}
}
